<?php

namespace Wap\Controller;

use Think\Page;
use User\Model\FactoryModel;
use Wap\Service\WechatService;
use Wap\Service\WeixinService;
use Wap\Model\StatisticsModel;

//header('Access-Control-Allow-Origin: *');

class Applications1Controller extends WapController
{
    public $wxTestToken;
    public $wxOfficialToken;
    public $wxTestUser;
    public $wxOfficialUser;
    private $_yuandan;
    private $_wennuanzhe;
    public $wxZLWDToken;
    public $wxGRZSToken;

    public function __construct()
    {
        parent::__construct();
        $this->wxTestToken = 'iequde1415960341';        //浙报新媒体
        $this->wxOfficialToken = 'wyirfc1427248117';    //浙报公益
        $this->wxZLWDToken = 'zuymbt1418285978';
        $this->wxGRZSToken = '21eb840870def11d6ef61fcf0cf60090';    //光荣浙商
        $this->handleCriteriaWxuser();
        $this->_yuandan = -11;
        $this->_wennuanzhe = -14;
    }

    //微信公众号信息
    private function handleCriteriaWxuser()
    {
        $this->wxTestUser = WeixinService::getWxuser($this->wxTestToken);
        $this->wxOfficialUser = WeixinService::getWxuser($this->wxOfficialToken);
    }

    private function handleCriteriaURL($id = 'ljfl')
    {
        $openid = session('openid_' . $id);
        if (empty($openid)) {
            if (!isset($_GET['code'])) {
                $url = "http://" . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
                $wechat = new WechatService($this->wxTestUser['appid'], $this->wxTestUser['appsecret']);
                $url = $wechat->snsapiUserInfoLink($url, $this->wxTestUser['appid']);
                header("Location: " . $url);
                exit();
            } else {
                $wechat = new WechatService($this->wxTestUser['appid'], $this->wxTestUser['appsecret']);
                $out = $wechat->snsapiUserInfo($this->wxTestUser['appid'], $this->wxTestUser['appsecret'], $_GET['code']);
                if ($out != null) {
                    session('openid_' . $id, $out['openid']);
                    session('wxname_' . $id, $out['nickname']);
                    session('wximg_' . $id, $out['headimgurl']);
                }
            }
        }
    }

    public function dhy()
    {
        $this->display("Applications1/dhy/index");
    }

    public function jtvideo()
    {
        $this->display("Applications1/jtvideo/index");
    }

    //------------------------------------------------------全名健身开始----------------------------------------------
    public function qmjs()
    {
        $wx = WeixinService::getWxuser($this->wxTestToken);
        $wxJSApi = WeixinService::buildWXJSApi($wx);
        $wxConfig = array(
            'appId' => $wx['appid'],
            'timestamp' => $wxJSApi['timestamp'],
            'nonceStr' => $wxJSApi['noncestr'],
            'signature' => $wxJSApi['sha1']
        );
        $this->assign('wxConfig', $wxConfig);
        $cond['id'] = array("between", [6, 16]);
        $data_list = M("data_important")->where($cond)->field("id,rensheng_view")->order("rensheng_view desc")->select();
        $return_list = array();
        foreach ($data_list as $data) {
            $unit['id'] = $data['id'] - 5;
            $unit['click'] = $data['rensheng_view'];
            $return_list[] = $unit;
        }
        $this->assign("list", $return_list);
        $this->display("Applications1/qmjs/index");
    }

    //---获取投票数量--
    public function ajaxQmjs()
    {
        $cond['id'] = array("between", [6, 16]);
        $id = I("get.id", "", "");
        if (!empty($id)) {
            $cond['id'] = $id + 5;
        }
        $data_list = M("data_important")->where($cond)->field("id,rensheng_view")->order("rensheng_view desc")->select();
        $return_list = array();
        foreach ($data_list as $data) {
            $unit['id'] = $data['id'] - 5;
            $unit['click'] = $data['rensheng_view'];
            $return_list[] = $unit;
        }
        $this->ajaxReturn($return_list);
    }

    //---投票----
    public function ajaxQmjstp()
    {
        $cond['id'] = I("get.id", "", "");
        if (!empty($cond['id'])) {
            $cond['id'] = $cond['id'] + 5;
            $data = M("data_important")->where($cond)->find();
        }
        $count = 0;
        if (!empty($data)) {
            $data['rensheng_view'] = $data['rensheng_view'] + 1;
            M("data_important")->where($cond)->save($data);
            $count = $data['rensheng_view'];
        }
        $this->ajaxReturn($count);
    }
    //-----------------------------------------------全明健身结束--------------------------------------------------

    //------------------------------------------选择题工具开始----------------------------------------------------
    public function select()
    {
        $id = I("get.id", "", "");
        if (empty($id)) {
            header("Content-type: text/html; charset=utf-8");
            die('找不到该活动！');
        }
        $cond['id'] = $id;
        $cond['status'] = 1;//----正常---
        $tool_ans = M("tool_ans")->where($cond)->find();
        if (empty($tool_ans)) {
            header("Content-type: text/html; charset=utf-8");
            die('找不到该活动！！');
        }
        $this->assign("title", "财富话题");
        $tool_ans = json_encode($tool_ans);
        $this->assign("data", $tool_ans);

        M("tool_ans")->where($cond)->setInc("view_total");

        $cond_hot['status'] = 1;
        $data_hot = M("tool_ans")->where($cond_hot)->order("view_total desc")->limit(1)->select();
        $data_hot = json_encode($data_hot);
        $this->assign("data_hot_list", $data_hot);//-----热门话题---


        $cond['id'] = array("LT", $tool_ans['id']);
        $data_rel_list = M("tool_ans")->where($cond)->order('id desc')->limit(3)->select();
        $data_rel_list = json_encode($data_rel_list);
        $this->assign("data_rel_list", $data_rel_list);//----更多话题---

        $wx = WeixinService::getWxuser($this->wxTestToken);
        $wxJSApi = WeixinService::buildWXJSApi($wx);
        $wxConfig = array(
            'appId' => $wx['appid'],
            'timestamp' => $wxJSApi['timestamp'],
            'nonceStr' => $wxJSApi['noncestr'],
            'signature' => $wxJSApi['sha1']
        );
        $this->assign('wxConfig', $wxConfig);
        $this->display("Applications1/toolAns/index");
    }

    public function selectAdd()
    {
        $id = I("get.id", "", "");
        $data_a = "50.00%";
        $data_b = "50.00%";
        $data_c = 0;
        $data_d = 0;
        if (empty($id)) {
            $this->ajaxReturn(['a' => $data_a, 'b' => $data_b, 'c' => $data_c, 'd' => $data_d]);
        }
        $cond['id'] = $id;
        $tool_ans = M("tool_ans")->where($cond)->find();
        if (empty($tool_ans)) {
            $this->ajaxReturn(['a' => $data_a, 'b' => $data_b, 'c' => $data_c, 'd' => $data_d]);
        }
        $select = I("get.select", "", "");
        if ($select) {
            $tool_ans['b_select']++;
        } else {
            $tool_ans['a_select']++;
        }
        $data_a = round($tool_ans['a_select'] * 100 / ($tool_ans['a_select'] + $tool_ans['b_select']), 2);
        $data_a = sprintf("%.2f", $data_a);
        $data_b = sprintf("%.2f", 100 - $data_a);
        $data_a .= "%";
        $data_b .= "%";
        $data['a_select'] = $tool_ans['a_select'];
        $data['b_select'] = $tool_ans['b_select'];
        $data['id'] = $id;
        M("tool_ans")->where($cond)->save($data);
        $this->ajaxReturn(['a' => $data_a, 'b' => $data_b, 'c' => $tool_ans['a_select'], 'd' => $tool_ans['b_select']]);
    }
    //------------------------------------------选择题工具结束----------------------------------------------------

    //-----------------------垃圾分类开始-------------------------
    public function ljfl()
    {
        $wx = WeixinService::getWxuser($this->wxTestToken);
        $this->handleCriteriaURL();
        $wxJSApi = WeixinService::buildWXJSApi($wx);
        FactoryModel::viewPage(17);
        $wxConfig = array(
            'appId' => $wx['appid'],
            'timestamp' => $wxJSApi['timestamp'],
            'nonceStr' => $wxJSApi['noncestr'],
            'signature' => $wxJSApi['sha1']
        );
        $this->assign('wxConfig', $wxConfig);

        $openid = session("openid_ljfl");
//        $openid = 123;
        if (empty($openid)) {
            header("Content-type: text/html; charset=utf-8");
            die("用户需要接受微信公众号鉴权！");
        }
        $date = date("Y-m-d");

        $cond['openid'] = $openid;
        $cond['date'] = $date;
        $user = M("ljfl_h5")->where($cond)->find();
        if (empty($user)) {//---新用户---
            $cond_old['openid'] = $openid;
            $data = M("ljfl_h5")->where($cond_old)->find();
            if (empty($data)) {
                $data['openid'] = $openid;
                $data['time'] = 60;
                $data['date'] = $date;
                $data['score'] = 0;
                $data['award1'] = 0;
                $data['award2'] = 0;
                $data['award3'] = 0;
                $data['award4'] = 0;
                $data['status'] = 0;
                $data['number'] = 0;
                $data['cishu'] = 0;
                $data['readed'] = 0;
                M("ljfl_h5")->add($data);
            } else {
                $data['time'] = 60;
                $data['date'] = $date;
                $data['award1'] = 0;
                $data['award2'] = 0;
                $data['award3'] = 0;
                $data['award4'] = 0;
                $data['status'] = 0;
                $data['cishu'] = 0;
                $data['number'] = 0;
                $data['readed'] = 0;
                $cond_old_save['id'] = $data['id'];
                M("ljfl_h5")->where($cond_old_save)->save($data);
            }

            $data['img'] = session("wximg_ljfl");
//            $data['img'] = "http://www.baidu.com";
            $user = json_encode($data);
            $this->assign("user", $user);

            $award = array();
            $award = json_encode($award);
            $this->assign("award", $award);
        } else {//--------已有用户-----------
            $user['img'] = session("wximg_ljfl");
//            $user['img'] = "http://www.baidu.com";

            $user = json_encode($user);
            $this->assign("user", $user);

            $cond_award['openid'] = $openid;
            $cond_award['pid'] = array("IN", [37, 38, 39]);
            $cond_award['zhongjiang'] = 1;
            $cond_award['status'] = 1;
            $award = M("redpacket_fansaward")->where($cond_award)->field("pid,award_id,createdate")->select();
            if (empty($award)) {
                $award = json_encode($award);
                $this->assign("award", $award);
            } else {
                $award_id_arr = array();
                foreach ($award as $val) {
                    $award_id_arr[] = $val['award_id'];
                }
                $award_id_arr = array_unique($award_id_arr);
                $cond_item['id'] = array("IN", $award_id_arr);
                $award_item_list = M("redpacket_item")->where($cond_item)->getField("id, redpacket_id, name, pic");
                foreach ($award as &$val) {
                    if (array_key_exists($val['award_id'], $award_item_list)) {
                        $val['name'] = $award_item_list[$val['award_id']]['name'];
                        if ($award_item_list[$val['award_id']]['redpacket_id'] == 37) {
                            $val['pic'] = "http://" . $_SERVER['HTTP_HOST'] . "/Public/projects/applications1/ljfl/statics/award1.png";
                        } elseif ($award_item_list[$val['award_id']]['redpacket_id'] == 38) {
                            $val['pic'] = "http://" . $_SERVER['HTTP_HOST'] . "/Public/projects/applications1/ljfl/statics/award2.png";
                        } else {
                            $val['pic'] = "http://" . $_SERVER['HTTP_HOST'] . "/Public/projects/applications1/ljfl/statics/award3.png";
                        }
                    } else {
                        $val['name'] = '';
                        $val['pic'] = '';
                    }
                }
                $award = json_encode($award);
                $this->assign("award", $award);
            }
        }
        //-------是否为助力------
        $sup_open_id = I("get.oid", "", "");
        if (!empty($sup_open_id) && ($sup_open_id != $openid)) {
            $cond_sup['openid'] = $sup_open_id;
            $cond_sup['sup_openid'] = $openid;
            $cond_sup['date'] = $date;
            $count = M("ljfl_sup")->where($cond_sup)->count();
            if ($count == 0) {
                $cond_user_suped['openid'] = $sup_open_id;
                $cond_user_suped['date'] = $date;
                $suped_user = M("ljfl_h5")->where($cond_user_suped)->find();
                if (($suped_user['number'] == 0) || ($suped_user['number'] == 2) || ($suped_user['number'] == 4)) {
                    if ($suped_user['time'] == 60) {
                        $suped_user['cishu']++;
                    } else {
                        $suped_user['time'] = 60;
                    }
                }
                $suped_user['number'] += 1;
                $cond_suped_save['id'] = $suped_user['id'];
                M("ljfl_h5")->where($cond_suped_save)->save($suped_user);

                $data_sup['openid'] = $sup_open_id;
                $data_sup['sup_openid'] = $openid;
                $data_sup['date'] = date("Y-m-d");
                M("ljfl_sup")->add($data_sup);
            } else {

            }
        }
        $this->display("Applications1/ljfl/index");
    }

    //---提交游戏结果---
    public function ajaxLjfl()
    {
        $time = I("post.time", "", "");//----游戏时间---
        $score = I("post.score", "", "");//------分数----
        $openid = session("openid_ljfl");
//        $openid = 123;
        $date = date("Y-m-d");
        $cond['openid'] = $openid;
        $cond['date'] = $date;
        $user = M("ljfl_h5")->where($cond)->find();
        if (empty($user)) {
            //----用户数据错误----
            $this->ajaxReturn(["code" => -1]);
        }
        if ($time < 0) {
            //----用户时间不足----
            $this->ajaxReturn(["code" => -2]);
        }
        if ($user['time'] < $time) {
            //----用户时间不足----
            $this->ajaxReturn(["code" => -2]);
        }
        if ($user['cishu'] > 0) {
            $user['cishu']--;
            $user['time'] = 60;
        } else {
            $user['time'] = 0;
        }
        $user['score'] = $score;
        $user['status'] = 1;
        $cond_save['id'] = $user['id'];
        M("ljfl_h5")->where($cond_save)->save($user);
        $this->ajaxReturn(["code" => 1]);
    }

    //----阅读科普知识增加抽奖机会----
    public function ajaxLjflRead()
    {
        $openid = session("openid_ljfl");
//        $openid = 123;
        $date = date("Y-m-d");
        $cond['openid'] = $openid;
        $cond['date'] = $date;
        $user = M("ljfl_h5")->where($cond)->find();
        if (!empty($user) && ($user['readed'] == 0)) {
            if ($user['time'] == 0) {
                $user['time'] = 60;
            } else {
                $user['cishu']++;
            }
            $user['readed'] = 1;
            $cond_save['id'] = $user['id'];
            M("ljfl_h5")->where($cond_save)->save($user);
        }
        $this->ajaxReturn(["code" => 1]);
    }

    //------积分兑换----
    public function ajaxLjflCharge()
    {
        $type = I("post.type", "", "");//---兑换分数---

        $openid = session("openid_ljfl");
//        $openid = 123;
        $date = date("Y-m-d");
        $cond['openid'] = $openid;
        $cond['date'] = $date;
        $user = M("ljfl_h5")->where($cond)->find();
        if (empty($user)) {
            //----用户数据错误----
            $this->ajaxReturn(["code" => -1]);
        }
        $score = 0;
        switch ($type) {
            case 1:
                $user['award1'] = 1;
                $score = 15;
                break;
            case 2:
                $user['award2'] = 1;
                $score = 80;
                break;
            case 3:
                $user['award3'] = 1;
                $score = 150;
                break;
            case 4:
                $user['award4'] = 1;
                $score = 200;
                break;
            default:
                //----奖品类型不正确----
                $this->ajaxReturn(["code" => -2]);
                break;
        }
        if ($user['score'] < $score) {
            //----y用户分数不够兑换----
            $this->ajaxReturn(["code" => -3]);
        }
        $user['score'] -= $score;
        $user['status'] = 1;
        $cons_save['id'] = $user['id'];
        M("ljfl_h5")->where($cons_save)->save($user);
        $this->ajaxReturn(["code" => 1]);
    }

    //-----------------------垃圾分类结束-------------------------
    public function h5data()
    {
        file_get_contents("http://esm.zbxinmeiti.com/User/Statistics/set_viewpage");
        $datas = M("data_important")->select();
        var_dump($datas);
    }

    //-------------信息分类开始-----------------
    public function xxfl()
    {
        $wx = WeixinService::getWxuser($this->wxTestToken);
        $wxJSApi = WeixinService::buildWXJSApi($wx);
        $wxConfig = array(
            'appId' => $wx['appid'],
            'timestamp' => $wxJSApi['timestamp'],
            'nonceStr' => $wxJSApi['noncestr'],
            'signature' => $wxJSApi['sha1']
        );
        $this->assign('wxConfig', $wxConfig);
        $this->display("Applications1/xxfl/index");
    }

    public function xxflAjax()
    {
        $params = I("post.", "", "");
        $msg = array("code" => 1, "massage" => '成功');
        if (empty($params['name']) || empty($params['phone'])) {
            $msg['code'] = -1;
            $msg['massage'] = "请填写联系方式";
            $this->ajaxReturn($msg);
        }

        $data['name'] = $params['name'];
        $data['phone'] = $params['phone'];
        $data['address'] = $params['address'];
        $data['type'] = $params['type'];
        $data['typed'] = $params['typed'];
        $data['desc'] = $params['desc'];
        $data['delivery'] = $params['delivery'];
        M("xxfl_order")->add($data);
        $msg = "浙报信息，有新的用户填写信息广告，联系电话：" . $data['phone'] . ",姓名:" . $data['name'];
        $result = FactoryModel::sendMsg("15657128579", $msg);
        $this->ajaxReturn($msg);

    }
    //-------------信息分类结束-----------------
    //----淘志愿学校信息2020---
    public function collApplication()
    {
        $wx = WeixinService::getWxuser($this->wxTestToken);
        $wxJSApi = WeixinService::buildWXJSApi($wx);
        $wxConfig = array(
            'appId' => $wx['appid'],
            'timestamp' => $wxJSApi['timestamp'],
            'nonceStr' => $wxJSApi['noncestr'],
            'signature' => $wxJSApi['sha1']
        );
        $this->assign('wxConfig', $wxConfig);
        $this->display("Applications1/collApplication/collApplication");
    }

    //-----“美丽浙江十佳优秀案例”网络评选-----
    public function mlzjsj(){
        $wx = WeixinService::getWxuser($this->wxTestToken);
        $wxJSApi = WeixinService::buildWXJSApi($wx);
        $wxConfig = array(
            'appId' => $wx['appid'],
            'timestamp' => $wxJSApi['timestamp'],
            'nonceStr' => $wxJSApi['noncestr'],
            'signature' => $wxJSApi['sha1']
        );
        $this->assign('wxConfig', $wxConfig);
        $this->display("Applications1/mlzjsj/index");
    }
}